package com.ktjy.controller;

import com.ktjy.entity.User;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class HelloController {
    @RequestMapping("/hello")
    public String hello(Model model,HttpSession session) {
        model.addAttribute("msg", "hello thymeleaf");

        model.addAttribute("user",new User(1,"李四","男",18));
        session.setAttribute("name","lisi");
        model.addAttribute("date",new Date());

        return "hello";
    }

    @RequestMapping("/expression")
    public String expression(Model model,HttpServletRequest request, HttpSession session){
        model.addAttribute("user",new User(1,"李四","男",18));

        return "expression";
    }

    @RequestMapping("/link")
    public String link(){
        return "link";
    }

    //链接表达式
    @RequestMapping("/save")
    @ResponseBody
    public User save(User user,Model model){
        return user;
    }

    @RequestMapping("/ifunless")
    public String ifunless(Model model){
        model.addAttribute("age",16);
        return "ifunless";
    }

    @RequestMapping("/switch2")
    public String switch2(Model model){
        model.addAttribute("sex","1");
        return "switch";
    }

    @RequestMapping("/each")
    public String each(Model model) {
        List<User> list=new ArrayList<>();
        list.add(new User(1,"张三","男",18));
        list.add(new User(2,"李四","女",19));
        list.add(new User(3,"王五","男",20));
        list.add(new User(4,"赵六","男",21));

        model.addAttribute("list",list);

        return "each";
    }

    @RequestMapping("/inline")
    public String inline(Model model){
        model.addAttribute("name","张三");
        return "inline";
    }

    @RequestMapping("/frame")
    public String frame(){
        return "frame";
    }

    @RequestMapping("/list")
    public String list(Model model) {
        List<User> list=new ArrayList<>();
        list.add(new User(1,"张三","男",18));
        list.add(new User(2,"李四","女",19));
        list.add(new User(3,"王五","男",20));
        list.add(new User(4,"赵六","男",21));

        model.addAttribute("list",list);

        return "list";
    }
}